package cn.amaake.magicplugin.satoken.autoconfigure;

import cn.amaake.magicplugin.satoken.Interceptor.CustomAuthorizationInterceptor;
import cn.amaake.magicplugin.satoken.Interceptor.CustomRequestInterceptor;
import cn.amaake.magicplugin.satoken.config.MagicApiSaTokenConfig;
import cn.amaake.magicplugin.satoken.filter.TokenResponseHeaderFilter;
import cn.dev33.satoken.config.SaTokenConfig;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.ssssssss.magicapi.core.config.MagicAPIProperties;
import org.ssssssss.magicapi.core.config.MagicPluginConfiguration;
import org.ssssssss.magicapi.core.model.Plugin;

@Configuration
@EnableConfigurationProperties(MagicApiSaTokenConfig.class)
public class MagicAPISaTokenConfiguration implements MagicPluginConfiguration {

    private final MagicAPIProperties properties;
    private final ApplicationContext applicationContext;
    private final MagicApiSaTokenConfig magicApiSaTokenConfig;

    public MagicAPISaTokenConfiguration(MagicAPIProperties properties, MagicApiSaTokenConfig magicApiSaTokenConfig, ApplicationContext applicationContext){
        this.properties = properties;
        this.magicApiSaTokenConfig = magicApiSaTokenConfig;
        this.applicationContext = applicationContext;
    }

    @Bean
    public TokenResponseHeaderFilter tokenResponseHeaderFilter(){
        return new TokenResponseHeaderFilter(magicApiSaTokenConfig);
    }


    @Bean
    @Primary
    public SaTokenConfig getSaTokenConfigPrimary() {
        SaTokenConfig config = magicApiSaTokenConfig.getSaToken();
        return config;
    }



    @Bean
    public CustomAuthorizationInterceptor customAuthorizationInterceptor(){
        return new CustomAuthorizationInterceptor(magicApiSaTokenConfig,applicationContext);
    }

    @Bean
    public CustomRequestInterceptor customRequestInterceptor(){
        return new CustomRequestInterceptor(magicApiSaTokenConfig,applicationContext);
    }



    @Override
    public Plugin plugin() {
        return new Plugin("SaToken鉴权插件");
    }
}
